package com.example.task1.ui.AllService.law;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.base.BaseActivity;
import com.example.task1.bean.LawDetailBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.HashMap;

import retrofit2.Call;

public class LawDetailActivity extends BaseActivity {

    private TextView tvName;
    private ImageView image;
    private ImageView carImage;
    private TextView tvLegalExpertiseName;
    private TextView tvEduInfo;
    private TextView tvLicenseNo;
    private TextView tvWorkStartAt;
    private TextView tvServiceTimes;
    private TextView tvFavorableRate;
    private int id;
    private Button btnSave;
    private LawDetailBean.DataDTO date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_law_detail);
        initView();
        id = getIntent().getIntExtra("id", 0);
        initData();
        Business();
    }

    private void Business() {
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(LawDetailActivity.this);
                LinearLayout linearLayout = new LinearLayout(LawDetailActivity.this);
                EditText et_content = new EditText(LawDetailActivity.this);
                EditText et_phone = new EditText(LawDetailActivity.this);
                et_content.setHint("内容");
                et_phone.setHint("电话");
                linearLayout.addView(et_content);
                linearLayout.addView(et_phone);
                builder.setView(linearLayout);
                builder.setTitle("请填写相关信息");
                builder.setNegativeButton("提交", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String content = et_content.getText().toString();
                        String phone = et_phone.getText().toString();
                        if (TextUtils.isEmpty(content) || TextUtils.isEmpty(phone)) {
                            MyApplication.show("内容和电话不能为空");
                        } else {
                            HashMap<String, Object> map = new HashMap<>();
                            map.put("lawyerId", date.getId());
                            map.put("legalExpertiseId", date.getLegalExpertiseId());
                            map.put("content", content);
                            map.put("phone", phone);
                            Call<ApiResp> post = Api.api().post(MyApplication.TOKEN, "/prod-api/api/lawyer-consultation/legal-advice", Api.getBody(map));
                            Api.send(post, ApiResp.class, new CallBack<ApiResp>() {
                                @Override
                                public void success(ApiResp data) {
                                    MyApplication.show("提交咨询成功");
                                }

                                @Override
                                public void error(String msg) {

                                }
                            });
                        }
                    }
                });
                builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.show();
            }
        });
    }

    private void initData() {
        Call<ApiResp> call = Api.api().get(MyApplication.TOKEN, "/prod-api/api/lawyer-consultation/lawyer/" + id);
        Api.send(call, LawDetailBean.class, new CallBack<LawDetailBean>() {
            @Override
            public void success(LawDetailBean data) {
                date = data.getData();
                MyApplication.glide(image, date.getAvatarUrl());
                MyApplication.glide(carImage, date.getCertificateImgUrl());
                tvEduInfo.setText("律师教育背景：" + date.getEduInfo());
                tvFavorableRate.setText("好评率：" + date.getFavorableRate());
                tvLegalExpertiseName.setText("擅长的法律专长名称：" + date.getLegalExpertiseName());
                tvName.setText(date.getName());
                tvWorkStartAt.setText("工作起始时间：" + date.getWorkStartAt());
                tvLicenseNo.setText("执业证编号：" + date.getLicenseNo());
                tvServiceTimes.setText("咨询人数：" + date.getServiceTimes());

            }

            @Override
            public void error(String msg) {

            }
        });
    }

    @Override
    public String getMyTitle() {
        return "律师详情";
    }

    private void initView() {
        tvName = findViewById(R.id.tv_name);
        image = findViewById(R.id.image);
        carImage = findViewById(R.id.car_image);
        tvLegalExpertiseName = findViewById(R.id.tv_legalExpertiseName);
        tvEduInfo = findViewById(R.id.tv_eduInfo);
        tvLicenseNo = findViewById(R.id.tv_licenseNo);
        tvWorkStartAt = findViewById(R.id.tv_workStartAt);
        tvServiceTimes = findViewById(R.id.tv_serviceTimes);
        tvFavorableRate = findViewById(R.id.tv_favorableRate);
        btnSave = findViewById(R.id.btn_save);
    }
}